package com.android.youwenchuang.ui.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.android.youwenchuang.R;
import com.android.youwenchuang.base.BaseActivity;
import com.android.youwenchuang.bean.CarBean;
import com.android.youwenchuang.databinding.ActivityCarBinding;
import com.android.youwenchuang.presenter.CarPresenter;
import com.android.youwenchuang.presenter.GoodsDetailsPresenter;
import com.android.youwenchuang.ui.adapter.CarAdapter;
import com.blankj.utilcode.util.ThreadUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.List;

/**
 * 购物车
 */
public class CarActivity extends BaseActivity {

    private ActivityCarBinding viewBinding;

    private CarPresenter presenter = new CarPresenter();

    private CarAdapter adapter = new CarAdapter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding = ActivityCarBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        initTitleBar();

        viewBinding.rvList.setLayoutManager(new LinearLayoutManager(this));
        viewBinding.rvList.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.tv_del);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                CarBean carBean = (CarBean) adapter.getItem(position);
                presenter.delete(carBean);
                adapter.removeAt(position);
            }
        });
        ThreadUtils.executeByIo(new ThreadUtils.SimpleTask<List<CarBean>>() {
            @Override
            public List<CarBean> doInBackground() throws Throwable {
                return presenter.getList();
            }

            @Override
            public void onSuccess(List<CarBean> result) {
                adapter.setNewInstance(result);
            }
        });
    }

}
